package com.jiesone.logger.distributecleaning.config;
/*
 *@ClassName RedisCacheConfig
 *@Description TODO
 *@Author tangsixiang@163.com
 *@Date 2022/1/18 15:11
 *@Version 1.0
 */

import com.jiesone.logger.distributecleaning.handler.ResolveLogDataRedisHandler;
import com.jiesone.logger.distributecleaning.handler.ResolveMachingRedisHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.concurrent.CountDownLatch;


@Configuration
public class RedisCacheConfig {

    Logger logger= LoggerFactory.getLogger(RedisCacheConfig.class);


    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            MessageListenerAdapter listenerAdapter1,
                                            MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        //订阅了一个叫chat的通道
        container.addMessageListener(listenerAdapter1, new PatternTopic("Loger:dataResolve"));
        //container 可以添加多个 messageListener
        container.addMessageListener(listenerAdapter, new PatternTopic("loger:machingResolve"));

        return container;
    }


    /**
     * 绑定消息监听者和接收监听的方法
     *
     * @param receiver
     * @return
     */


    @Bean
    MessageListenerAdapter listenerAdapter1(ResolveLogDataRedisHandler receiver) {
        //给messageListenerAdapter 传入一个消息接受的处理器，利用反射的方法调用“receiveMessage”
        //不填defaultListenerMethod默认调用handleMessage
        logger.info("receiver ResolveLogDataRedisHandler 监听适配器生效了 = " + receiver);
        return new MessageListenerAdapter(receiver, "receiverMessage");
    }

    @Bean
    MessageListenerAdapter listenerAdapter(ResolveMachingRedisHandler receiver) {
        //给messageListenerAdapter 传入一个消息接受的处理器，利用反射的方法调用“receiveMessage”
        //不填defaultListenerMethod默认调用handleMessage
        logger.info("receiver ResolveMachingRedisHandler  监听适配器生效了 = " + receiver);
        return new MessageListenerAdapter(receiver, "receiverMessage");
    }



    /**
     * 注册订阅者
     * @param latch
     * @return
     */
    @Bean
    ResolveLogDataRedisHandler receiver(CountDownLatch latch) {
        return new ResolveLogDataRedisHandler(latch);
    }

    /**
     * 计数器，用来控制线程
     * @return
     */
    @Bean
    public CountDownLatch latch(){
        return new CountDownLatch(1);//指定了计数的次数 1
    }


    /**
     * 读取内容的template
     */

    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }
}